
<template>
	<view class="search-container">
      <view class="search-box">
        <uni-search-bar @confirm="" @input="input" :radius="18" cancelButton="none"/>
      </view>        
    
    <view class="search-tab">
      <view class="history">
        <view class="">
          搜索历史
        </view>
        <uni-icons type="trash" size="18" @click='del'></uni-icons>
      </view>
      
      <!-- 搜索历史栏 -->
    <view class="search-history-list" v-if="searchList.length === 0">
        <uni-tag :text="item" v-for="(item,i) in histories" :key='i' class="tag"></uni-tag>
      </view>
    </view>
    
    <!-- 搜索的列表 -->
    <!-- <view class=""  v-else> -->
      <view class="search-list" v-for="(item,i) in searchList" :key='i'>
          <view class="search-item" @click="gotoGoodsDetail(item)">
            <view class="content">
              {{item.goods_name}}
            </view>
            <uni-icons type="arrowright" size='16'></uni-icons>
      </view>
    </view>
    <!-- </view> -->
    
	</view>
</template>

<script>
	export default {
		data() {
			return {
				kw:'',
        timer:null,
        searchList:[],
        historyList:['a','app','aaple']
			}
		},
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]' )
    },
    computed:{
      histories(){
        return [...this.historyList].reverse()
      }
    },
    methods:{
      input(e){
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.kw = e.value
          this.getSearchList()
        },1500)
      },
      
      
      // 获取搜索结果
      async getSearchList(){
        if(this.kw === '') {
          this.searchList = []
          return
        }
        const {data : res} = await uni.$http.get('/goods/qsearch',{query:this.kw})
        if(res.meta.status !== 200) return uni.$showMsg()
        this.searchList = res.message
        this.saveSearchHistory()
      },
      
      // 去商品的详情页面
      gotoGoodsDetail(e){
        uni.navigateTo({
          url:'/subpkg/goods_detail/goods_detail?goods_id=' + e.goods_id
        })
      },
      
      // 存储搜索历史
      saveSearchHistory(){
        // this.historyList.push(this.kw)
        // 首先设置一个set数组
        const set = new Set(this.historyList)
        // 删除数组中的
        set.delete(this.kw)
        // 添加数组元素,这样子做能够保证元素的顺序
        set.add(this.kw)
        this.historyList = Array.from(set)
        // 持久存储到本地存储中
        uni.setStorageSync('kw',JSON.stringify(this.historyList))
      },
      
      // 删除搜索历史
      del(){
        // console.log('111111111111')
        this.historyList = []
        uni.setStorageSync('kw','')
        
      }
      
    }
	}
</script>

<style lang="scss">
.search-container{

.search-tab{
  .history{
    display: flex;
    height: 30px;
    padding: 10px;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid #4CD964;
  }
  
  .search-history-list{
    display: flex;
    flex-wrap: wrap;
    .tag{
      // width: 70rpx;
      margin: 2px;
      border: 1px solid #fff;
    }
  }
}


.search-list{
  .search-item{
    height: 40rpx;
    width: 96%;
    border-bottom: 1px solid #efefef;
    padding: 10px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .content{
      white-space: nowrap;
      overflow: hidden;
      text-overflow: ellipsis;
      width: 90%;
     }
    }
  }

}

</style>
